import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};

public class Solution {
    public List<List<Integer>> levelOrder(Node root) {

        Queue<Node> q = new LinkedList<>();

        List<List<Integer>> ret = new ArrayList<>();

        if (root == null) {
            return ret;
        }

        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            List<Integer> temp = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node t = q.poll();
                temp.add(t.val);
                for (Node node : t.children) {
                    if (node != null) {
                        q.offer(node);
                    }
                }
            }
            ret.add(temp);
        }

        return ret;
    }
}
